package org.tsinghua.answer.domain.question;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tsinghua.answer.domain.question.exception.ReplyException;
import org.tsinghua.answer.domain.user.User;
import org.tsinghua.answer.domain.user.UserRepository;
import org.tsinghua.answer.utils.Identities;
import org.tsinghua.answer.utils.PageableBuilder;

@Transactional
@Service
public class QuestionService {

	@Autowired
	private QuestionRepository questionRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private ReplyRepository replyRepository;
	
	public Question createQuestion(Question question) {
		question.setId(Identities.uuid2());
		User creator = userRepository.findOne(question.getCreatorId());
		question.setCreator(creator);
		return questionRepository.save(question);
	}
	
	public Page<Question> userQuestions(String creatorId, String keyword, QuestionStatus status, int page, int size) {
		Pageable pageable = PageableBuilder.build(page, size);
		if(StringUtils.isBlank(keyword)) {
			keyword = "%";
		} else {
			keyword = "%" + keyword + "%";
		}
		return questionRepository.userQuestions(creatorId, keyword, status, pageable);
	}
	
	public Page<Question> volunteerQuestions(String volunteerId, boolean answered, String subjectId,
			String keyword, int page, int size) {
		Pageable pageable = PageableBuilder.build(page, size);
		if(StringUtils.isBlank(keyword)) {
			keyword = "%";
		} else {
			keyword = "%" + keyword + "%";
		}
		if(answered) {
			if(StringUtils.isBlank(subjectId)) {
				return questionRepository.volunteerQuestions(volunteerId, keyword, pageable);
			} else {
				return questionRepository.volunteerQuestions(volunteerId, subjectId, keyword, pageable);
			}
		} else {
			if(StringUtils.isBlank(subjectId)) {
				return questionRepository.findByStatus(QuestionStatus.WAITING, keyword, pageable);
			} else {
				return questionRepository.findByStatusAndSubject(QuestionStatus.WAITING, subjectId, keyword, pageable);
			}
		}
	}
	
	public Reply reply(Reply reply) throws ReplyException {
		String content = reply.getContent();
		if(StringUtils.isBlank(content)) {
			throw new ReplyException("不能回复空内容。");
		}
		User creator = userRepository.findOne(reply.getCreatorId());
		Question question = questionRepository.findOne(reply.getQuestionId());
		if(!question.getCreator().getId().equals(creator.getId())) {
			//设置为已回复
			question.setStatus(QuestionStatus.ANSWERED);
			questionRepository.save(question);
		}
		reply = new Reply(creator, question, content);
		reply = replyRepository.save(reply);
		return reply;
	}
	
	/**
	 * 关闭问题（已经懂了）
	 * @param questionId
	 */
	public void close(String questionId) {
		Question question = questionRepository.findOne(questionId);
		question.setStatus(QuestionStatus.CLOSED);
		questionRepository.save(question);
	}
}
